let BangProp =require("BangProp");
let GameControl =require("GameControl");
cc.Class({
    extends: cc.Component,

    properties: {
        //是铁
        isIron:false,

        //碰撞几次销毁
        needDesCount:1,
        //所加分数
        squareScore:1,
        //下落速度
        dropSpeed:100,
        //是不是道具
        isProp:false,
        //道具名字
        propName:"",
        _rigid:{
            default:null,
            type:cc.RigidBody,
        },
        _box:{
            default:null,
            type:cc.PhysicsBoxCollider,
        },
        _triggerBox:{
          default:null,
          type:cc.BoxCollider,
        },

        _label:{
            default:null,
            type:cc.Label,
        },



    },

    // LIFE-CYCLE CALLBACKS:

     onLoad () {
         //cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);
     },


    onKeyUp:function(event){

        switch(event.keyCode){
            case cc.KEY.num9:
                cc.log("测试生产");
                if(this.isProp){

                    let pos = this.node.parent.convertToWorldSpace(this.node.position);

                    let pName =this.propName;

                    cc.loader.loadRes("Prop",function (err, prefab) {
                        cc.log("test1");
                        let prop =cc.instantiate(prefab);
                        cc.director.getScene().addChild(prop);
                        let bp =prop.getComponent(BangProp);

                        bp.SendProp(pName);

                        prop.position=pos;


                    });

                    cc.log("test2");
                    this.Des();

                }

                break;
        }
    },

    start () {
        //cc.log(GameControl.instance);

        if(GameControl.instance===null)
        {
            cc.error("没有引用到");
        }
         this._label =this.getComponentInChildren(cc.Label);
         if(this._label===null&& this.isProp===false){

             let la =new cc.Node();
             la.parent =this.node;

            this._label =la.addComponent(cc.Label);
            this.node.color =cc.Color.BLACK;
            this._label.string=this.needDesCount.toString();
         }

         this._rigid= this.addComponent(cc.RigidBody);
         this._rigid.enabledContactListener =true;
         this._rigid.type=cc.RigidBodyType.Static;

         if(this.getComponent(cc.PhysicsPolygonCollider)!==null){
            return;
         }


         if(this.getComponent(cc.PhysicsBoxCollider)===null){
             this._box= this.addComponent(cc.PhysicsBoxCollider);
             this._box.size.width =50;
             this._box.size.height =50;
             this._box.apply();

         }
         if(this.getComponent(cc.BoxCollider)===null){
             this._triggerBox = this.addComponent(cc.BoxCollider);
             this._triggerBox.size =this._box.size;
         }
         //this._triggerBox.apply();

         //cc.log("sizi"+this._box.size.width);
         },




    onBeginContact: function (contact, selfCollider, otherCollider) {


    },

    // 只在两个碰撞体结束接触时被调用一次
    onEndContact: function (contact, selfCollider, otherCollider) {
    },

    // 每次将要处理碰撞体接触逻辑时被调用
    onPreSolve: function (contact, selfCollider, otherCollider) {
    },

    // 每次处理完碰撞体接触逻辑时被调用
    onPostSolve: function (contact, selfCollider, otherCollider) {

        //需要判定碰撞对象是不是ball
        if(this.isIron)
        {
            return;
        }

        if(otherCollider.node.name==="Ball"){
            this.GetHit();
        }


    },
    onEnable(){
        let thisObj =this;

        this.node.on('GetHit',function (event) {

            //cc.log("接受到事件");
            thisObj.GetHit();
        });

    },

    GetHit(){

        GameControl.instance.scoreCtl.AddScore(this.squareScore);
        this.needDesCount--;
        if(this._label!==null){

            this._label.string=this.needDesCount.toString();
        }

        if(this.isProp){

            //let pos = this.node.parent.convertToWorldSpace(this.node.position);
            //let pName="BossBall";
            // cc.loader.loadRes("Prop",function (err, prefab) {
            //
            //     for(let i=0;i<10;i++)
            //     {
            //         let prop =cc.instantiate(prefab);
            //         cc.director.getScene().addChild(prop);
            //         let bp =prop.getComponent(BangProp);
            //         bp.SendProp(pName);
            //         prop.position=pos;
            //
            //     }
            //
            // });


            let pos = this.node.parent.convertToWorldSpace(this.node.position);
            let pName =this.propName;
            cc.loader.loadRes(pName,function (err, prefab) {

               let prop =cc.instantiate(prefab);
               cc.director.getScene().addChild(prop);
               let bp =prop.getComponent(BangProp);

               bp.SendProp(pName);

               prop.position=pos;

            });


            this.Des();
            return;
        }
        if(this.needDesCount===0)
        {
            this.Des();
        }


    },

    Explode(targetpos,length2)//为平方长度
    {
        if(cc.pDistanceSQ(targetpos,this.node.position)<length2*length2){
            GameControl.instance.scoreCtl.AddScore(this.needDesCount*this.squareScore);
            this.Des();
        }

    },

    Des(){
        this.node.destroy();
    },


    //(update(dt))
    UpdateFun(){

        this.node.y-=this.dropSpeed;
    },

});
